from typing import *


class Solution:

    def pivotInteger(self, n: int) -> int:
        l, r = 1, n
        L, R = 1, n
        while l < r:
            if L <= R:
                l += 1
                L += l
            else:
                r -= 1
                R += r
        return l if L == R else -1
